package com.wsd.jing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.enums.CodeEnum;
import com.wsd.jing.exception.BusinessException;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.entity.Dictionary;
import com.wsd.jing.entity.DictionaryDetail;
import com.wsd.jing.mapper.DictionaryDetailMapper;
import com.wsd.jing.mapper.DictionaryMapper;
import com.wsd.jing.service.IDictionaryService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: DictionaryServiceImpl
 * @description: 数据字典主服务实现类
 * @createDate 2022-11-20 07:41
 * @copyright 2022-11-20 07:41 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {

    private final DictionaryMapper dictionaryMapper;
    private final DictionaryDetailMapper dictionaryDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertDictionary(Dictionary dictionary) {
        QueryWrapper<Dictionary> dictionaryQueryWrapper = new QueryWrapper<>();
        dictionaryQueryWrapper.lambda().eq(Dictionary::getType, dictionary.getType());
        Dictionary dictionary1 = this.getOne(dictionaryQueryWrapper);
        if (ObjectUtil.isNotEmpty(dictionary1)) {
            throw new BusinessException(CodeEnum.DICTIONARY_TYPE_EXIST);
        }
        dictionary.setCreateTime(LocalDateTime.now());
        return this.save(dictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDictionary(Dictionary dictionary) {
        QueryWrapper<Dictionary> dictionaryQueryWrapper = new QueryWrapper<>();
        dictionaryQueryWrapper.lambda().eq(Dictionary::getType, dictionary.getType());
        Dictionary dictionary1 = this.getOne(dictionaryQueryWrapper);
        if (ObjectUtil.isNotEmpty(dictionary1) && !dictionary.getId().equals(dictionary1.getId())) {
            throw new BusinessException(CodeEnum.DICTIONARY_TYPE_EXIST);
        }
        dictionary.setUpdateTime(LocalDateTime.now());
        return this.updateById(dictionary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDictionary(Integer id) {
        // TODO 删除时需判断在其他业务表中是否使用该记录
        QueryWrapper<DictionaryDetail> dictionaryDetailQueryWrapper = new QueryWrapper<>();
        dictionaryDetailQueryWrapper.lambda().eq(DictionaryDetail::getDictionaryId, id);
        dictionaryDetailMapper.delete(dictionaryDetailQueryWrapper);
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDictionarys(String ids) {
        // TODO 删除时需判断在其他业务表中是否使用该记录
        List<String> idList = StrUtil.splitTrim(ids, ",");
        QueryWrapper<DictionaryDetail> dictionaryDetailQueryWrapper = new QueryWrapper<>();
        dictionaryDetailQueryWrapper.lambda().in(DictionaryDetail::getDictionaryId, idList);
        dictionaryDetailMapper.delete(dictionaryDetailQueryWrapper);
        return this.removeByIds(idList);
    }

    @Override
    public Dictionary getDictionary(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<Dictionary> listDictionarys(CommonRequest commonRequest, Dictionary dictionary) {
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageDictionarys(CommonRequest commonRequest, Dictionary dictionary) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<Dictionary> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        if (StrUtil.isNotBlank(dictionary.getName())) {
            queryWrapper.lambda().like(Dictionary::getName, dictionary.getName());
        }
        if (StrUtil.isNotBlank(dictionary.getType())) {
            queryWrapper.lambda().like(Dictionary::getType, dictionary.getType());
        }
        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<Dictionary> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
}
